package com.ruoyi.web.controller.hospitals;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.hospital.service.DepartmentsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hospital.domain.Departments;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 科室信息Controller
 *
 * @author ruoyi
 * @date 2025-07-01
 */
@RestController
@RequestMapping("/departments/departments")
public class DepartmentsController extends BaseController {
    @Autowired
    private DepartmentsService departmentsService;

    /**
     * 根据医院ID获取科室列表
     */
    @PreAuthorize("@ss.hasPermi('appointments:departments:list')")
    @GetMapping("/selectDeptByHospitalId/{hospitalId}")
    public AjaxResult selectDeptByHospitalId(@PathVariable String hospitalId)
    {
        Departments query = new Departments();
        query.setHospitalId(hospitalId);
        List<Departments> list = departmentsService.selectDeptByHospitalId(query);
        return success(list);
    }

    @PreAuthorize("@ss.hasPermi('departments:departments:list')")
    @GetMapping("/getAllDepartments")
    public AjaxResult getAllDepartments(Departments departments) {

        return success(departmentsService.list());
    }

    /**
     * 查询科室信息列表
     */
    @PreAuthorize("@ss.hasPermi('departments:departments:list')")
    @GetMapping("/list")
    public TableDataInfo list(Departments departments) {
        // 调用父类的 startPage 方法进行分页
        startPage();
        // 调用服务层的方法获取科室信息列表
        List<Departments> list = departmentsService.selectDepartmentsList(departments);
        // 调用父类的 getDataTable 方法将结果封装成 TableDataInfo 对象
        System.out.println(list.size());
        TableDataInfo dataTable = getDataTable(list);
        long count = departmentsService.count();
        dataTable.setTotal(count);
        return dataTable;
    }

    /**
     * 导出科室信息列表
     */
    @PreAuthorize("@ss.hasPermi('departments:departments:export')")
    @Log(title = "科室信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Departments departments) {
        List<Departments> list = departmentsService.selectDepartmentsList(departments);
        ExcelUtil<Departments> util = new ExcelUtil<>(Departments.class);
        util.exportExcel(response, list, "科室信息数据");
    }

    /**
     * 获取科室信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('departments:departments:query')")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable("deptId") Long deptId) {
        System.out.println("接收到的 deptId: " + deptId);
        return success(departmentsService.selectDepartmentsByDeptId(deptId));
    }

    /**
     * 新增科室信息
     */
    @PreAuthorize("@ss.hasPermi('departments:departments:add')")
    @Log(title = "科室信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Departments departments) {
        return toAjax(departmentsService.insertDepartments(departments));
    }

    /**
     * 修改科室信息
     */
    @PreAuthorize("@ss.hasPermi('departments:departments:edit')")
    @Log(title = "科室信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Departments departments) {
        return toAjax(departmentsService.updateDepartments(departments));
    }

    /**
     * 删除科室信息
     */
    @PreAuthorize("@ss.hasPermi('departments:departments:remove')")
    @Log(title = "科室信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptId}")
    public AjaxResult remove(@PathVariable List<Long> deptId) {
        System.out.println("接收到的 deptIds: " + deptId);
        return toAjax(departmentsService.deleteDepartmentsByDeptIds(deptId));
    }
}